#pragma once

#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/enable_shared_from_this.hpp>

class NetSession : public boost::enable_shared_from_this<NetSession>, private boost::noncopyable
{
public:
    NetSession(boost::asio::io_service& service);
    virtual ~NetSession();

    virtual void Start();
    virtual void Close();

    virtual bool ReadPacket() = 0;

public:
    boost::asio::ip::tcp::socket& Socket() { 
        return socket_; 
    }

    void handle_read(const boost::system::error_code& error,
        size_t bytes_transferred);

    void handle_write(const boost::system::error_code& error);

protected:
    bool ReadBuffer(const char* buf, size_t bufLen);
    bool SendBuffer(const char* buf, size_t bufLen);

protected:
    std::string addr_;
    unsigned short port_;

    boost::asio::io_service::strand strand_;

    boost::asio::ip::tcp::socket socket_;

    boost::array<char, 8192> buffer_;

    boost::circular_buffer<char> recvBuf_;
    boost::circular_buffer<char> sendBuf_;
};
